public class Test {
    public static void main(String[] args) {
        Account account = new Account();
        Thread custum = new Custum(account);
        Thread maf = new maf(account);
        custum.start();
        maf.start();
    }
}

class Custum extends Thread {
    Account account;

    public Custum(Account account) {
        this.account = account;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (account.lock) {

                if (account.balance < 100) {
                    System.out.println("余额不足等待充值，通知妈妈充值");
                    try {
                        account.lock.wait();
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                } else {
                    account.balance -= 100;
                    System.out.println("儿子取出:" + 100 + "剩余:" + account.balance);
                    account.lock.notifyAll();
                }
            }
        }

    }
}

class maf extends Thread {

    Account account;

    public maf(Account account) {
        this.account = account;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (account.lock) {
                if (account.balance < 100) {
                    account.balance += 2000;
                    System.out.println("妈妈存了:" + 2000 + "余额为:" + account.balance);
                    account.lock.notifyAll();
                } else {
                    try {
                        account.lock.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }

    }
}
